<template>
  <div class="bk"
    style="width: 100%;height: 100vh;background: linear-gradient(90deg,#3cadeb,#9c88ff);display: flex;justify-content: center;align-items: center">
    <div
      style="width: 300px; height: 380px;box-shadow: 0 5px 15px rgba(0, 0, 0, .8); display: flex;justify-content: center;align-items: center;">
      <el-form :model="form" size="normal" style="height: 100% ;width: 220px;" ref="form" :rules="rules">
        <div style="width: 100%;height: 40px;margin-top:10px;line-height: 40px;text-align: center">
          <span style="color: white;font-weight: 900;font-size: 30px;">Login2</span>
        </div>
        <el-form-item style="margin-top: 50px" prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-user-solid" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <!-- <el-button
            style="color: white; border-radius: 30px;width: 100%;height: 50px;background: linear-gradient(90deg,#3cadeb,#9c88ff);"
            @click="login">登录
          </el-button> -->
          <el-button
            style="color: white; border-radius: 30px;width: 100%;height: 50px;background: linear-gradient(90deg,#3cadeb,#9c88ff);"
            @click="choose">登录
          </el-button>
        </el-form-item>




        <Modal title="选择协议类型" v-model="chooseModal" :mask-closable="false" ok-text="确定" @on-ok="selectP" draggable="true">
           <!-- 设置此modal对话框为课拖拽的  -->
           <Space wrap size="large">
        <RadioGroup v-model="button1" type="button" size="large">
            <Radio label="IMAP"></Radio>
            <Radio label="POP3"></Radio>
        </RadioGroup>
         </Space>
        </Modal>



        <el-form-item style="text-align: center; color: white;">
          <a @click="toLogin" style=" color:black;font-size: 16px;font-weight: 500;opacity: 0.8;text-decoration: underline">返回</a>
        </el-form-item>
        <el-form-item style="text-align: center; color: white;">
          <a @click="toindex" style=" color:black;font-size: 16px;font-weight: 500;opacity: 0.8;text-decoration: underline">去主页</a>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import global_variable from "@/api/global_variable"
export default {
  name: "VueLogin2.0",
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      },
      button1:"IMAP",
      chooseModal:false,
      isOK:false
    }
  },
  created() {
    sessionStorage.removeItem("user");
  },
  methods: {
    selectP(){
        this.$store.dispatch('email/setReceiveProtocol',{"Protocol":this.button1})
        .then(res => {
          console.log("后端返回")
          console.log(res)
          this.login();
          this.$message({
            type: "success",
            message: "设置成功"  
          })
        })
        .catch(error => {
          console.log(error)
          this.$message({
            type: "error",
            message: "失败"
          })
        })
    },
    toLogin() {
      this.$router.push("/Login")
    }, 
    choose(){
      this.chooseModal=true;
    },
    toindex(){
      this.$router.push("/index")
    },
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
        this.$store.dispatch('user/updateLogin',{"account":this.form.username,"password":this.form.password})
        .then(res => {
          console.log("后端返回")
          console.log(res)
          this.$message({
            type: "success",
            message: "登录成功"  
          })
          this.$router.push("/EmailSystemPages/SendEmail/Send")
        })
        .catch(error => {
          console.log(error)
          this.$message({
            type: "error",
            message: "失败"
          })
        })
          
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    
    }
  }
}
</script>

<style scoped>
.bk {
  background-image: url(@/assets/image/true.jpg);
}
</style>
